package bb.lanxing.model.database;


import android.content.ContentValues;
import android.util.Log;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.Iterator;
import java.util.List;

public class LitePalTableBase extends LitePalSupport {

    public Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public static <T> void deleteAll(Class<T> cls) {
        LitePal.deleteAll(cls);

    }

    public static <T> void deleteAll(Class<T> cls, String... params) {

        LitePal.deleteAll(cls, params);


    }

    public static <T> List<T> listAll(Class<T> cls) {

        List<T> l = LitePal.findAll(cls);

        return l;
    }

    public static <T> T findFirst(Class<T> cls) {

        T o = LitePal.findFirst(cls);
        return o;

    }

    public static <T> List<T> find(Class<T> cls, String where, String param) {

        List<T> list = LitePal.where(where, param).find(cls);
        return list;

    }

    public static <T> List<T> find(Class<T> cls, String where, String param1, String param2) {

        List<T> list = LitePal.where(where, param1, param2).find(cls);
        return list;

    }

    public static <T> List<T> find(Class<T> cls, String where, String param1, String param2, String param3) {

        List<T> list = LitePal.where(where, param1, param2, param3).find(cls);
        return list;

    }

    public static <T> T findById(Class<T> cls, long id) {

        T o = LitePal.where("id=", String.valueOf(id)).findFirst(cls);

        return o;
    }

    public static <T> T findById(Class<T> cls, Integer num) {

        T o = LitePal.where("id=", String.valueOf(num)).findFirst(cls);

        return o;
    }

    public static <T> Iterator<T> findAsIterator(Class<T> cls, String str, String... strArr) {
        return findAsIterator(cls, str, strArr, null, null, null);
    }

    public static <T> Iterator<T> findWithQueryAsIterator(Class<T> cls, String str, String... strArr) {
        return null;
    }

    public static <T> Iterator<T> findAsIterator(Class<T> cls, String str, String[] strArr, String str2, String str3, String str4) {
        return null;
    }

    public static long count(Class<?> cls) {

        return LitePal.count(cls);
    }

    @Override
    public boolean save() {

//        boolean b = super.save();
//        return b;
//        Log.d("LitePal", "hulin-save: id=" + id);
        if (id == null) {
            boolean b = super.save();
            return b;
        } else {
            int rows = update(id);
            if (rows > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    //this function return id
    public long save_() {

        long newId = -1;

        try {
            LitePal.beginTransaction();

            super.save();

            //////newly added by sst
            RouteBook newRouteBook = LitePal.where("id>?", "0").order("id DESC").findFirst(RouteBook.class);

            newId = newRouteBook.getId();
            /////////////////

            LitePal.setTransactionSuccessful();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LitePal.endTransaction();
        }


        return newId;

    }

    public <T extends LitePalTableBase> long saveReturnId() {
        long newId = -1;

        try {
            LitePal.beginTransaction();

            super.save();

            // 查询最新插入的数据，返回 id
            Class<T> clazz = (Class<T>) this.getClass();
            newId = LitePal.findLast(clazz).getId();

            LitePal.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LitePal.endTransaction();
        }

        return newId;
    }

    public void update(ContentValues contentValues) {
        Log.d("LitePal", "update: this.id=" + this.id);
        if (this.id == null) {
            return;
        }
        Log.d("LitePal", "update: contentValues=" + contentValues);
        LitePal.update(getClass(), contentValues, id);
    }
}